package javabase.schoolwork.day6_4;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author fance
 * @date 18-6-4 下午1:27
 */
public class Sum {

    // ==
    public void fun1() {
        String s1 = "hello";
        String s2 = new String("hello");
        String s3 = s2;

        System.out.println(s1 == s2);
        System.out.println(s1 == s3);
        System.out.println(s2 == s3);

        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s2.equals(s3));


        String s4 = "hello";
        String s5 = "hello";
        String s6 = "hello";
        System.out.println(s4 == s5 && s5 == s6);
    }

    public void runtime() {
        Runtime runtime = Runtime.getRuntime();
        try {
           Process process = runtime.exec("*.exe");

            Thread.sleep(3000);
            process.destroy();
        } catch (Exception e) {

        }
    }

    public void date() {
        Date date = new Date();
        System.out.println(date);

        System.out.println(System.currentTimeMillis());
        System.out.println(date.getTime());

        Date date1 = new Date(date.getTime());
        System.out.println(date1.getTime());
        System.out.println(date1.toString());

    }

    public String trim(String s) {
        if (s == null || s.length() < 1) {
            return s;
        }
        int st = 0;
        while (s.charAt(st) == ' ') {
            st++;
        }
        // now st-> 第一个非空格 同理
        int end = s.length() - 1;
        while (s.charAt(end) == ' ') {
            end--;
        }
        return s.substring(st,end + 1);
    }

    public String reverse(String s, int st,int end) {
        char[] chars = s.toCharArray();
        reverseCore(chars,st,end);
        return String.valueOf(chars);
    }
    private void reverseCore(char[] s,int st,int end) {
        if (st > end) {
            return;
        }
        while (st < end) {
            swap(s,st++,end--);
        }

    }
    private void swap(char[] a, int i, int j) {
        char tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }


    public int cntStrInOtherStr(String s1,String s2) {
        if (s1 == null || s1.length() < 1 || s2 == null || s2.length() < 1) {
            return 0;
        }
        int res = 0;
        int curIndex = 0;
        while (s1.contains(s2)) {
            res++;
            curIndex = s1.indexOf(s2);

            s1 = s1.substring(curIndex + s2.length());
        }
        return res;
    }

    public String reverseUseSB(String s) {
        String[] arr = s.split(" ");
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            stringBuilder.append(new StringBuilder(arr[i]).reverse()).append(" ");
        }
        return stringBuilder.toString().trim();
    }

    // 课堂练习2
    public void fun2(String s) {
        System.out.println(cntChar(s,'s'));
        char[] chars = s.toCharArray();

        System.out.println(getString(chars,4));
        System.out.println(makeWordBeginUpCase(chars));
    }
    private int cntChar(String s,char c) {
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            if (c == s.charAt(i)) {
                res++;
            }
        }
        return res;
    }
    private String getString(char[] s,int index) {
        int curSpace = 0;
        int st = 0;
        int end = 0;
        boolean flag = false;
        for (int i = 0; i < s.length; i++) {
            if (s[i] == ' ' || s[i] == ',') {
                curSpace++;
            }

            if (curSpace == index - 1 && !flag) {
                st = i;
                flag = true;
            }
            if (curSpace == index) {
                end = i;
                break;
            }


        }
        return String.valueOf(s).substring(st + 1,end);
    }

    private String makeWordBeginUpCase(char[] s) {
        for (int i = 0; i < s.length; i++) {
            if (i == 0 || s[i - 1] == ' ') {
                s[i] = (char) (s[i] - 32);
            }
        }
        return String.valueOf(s);
    }


    public void jvm() {
        Runtime runtime = Runtime.getRuntime();
        System.out.println(runtime.maxMemory());
        System.out.println(runtime.freeMemory());
        String s = "";
        for (int i = 0; i < 100; i++) {
            s += i;
        }
        System.out.println(runtime.freeMemory());
        System.gc();
        System.out.println(runtime.freeMemory());

    }

    public void os() {
        System.out.println(System.getProperty("os.name"));
        System.out.println(System.getProperty("os.version"));
        System.out.println(System.getProperty("os.arch"));
        System.out.println(System.getProperty("user.name"));
        System.out.println(System.getProperty("user.home"));
        System.out.println(System.getProperty("user.dir"));
    }

    public void calendar() {
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();

        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,4);

        System.out.println(calendar.getTime());

        calendar.add(Calendar.HOUR,2);
        System.out.println(calendar.getTime());
        calendar.add(Calendar.MONTH,-2);
        System.out.println(calendar.getTime());

    }

    public void simpleDateFormat() {
        Date date = new Date();

        SimpleDateFormat formater = new SimpleDateFormat();
        System.out.println(formater.format(date));

        SimpleDateFormat formater1 = new SimpleDateFormat("yyyy年MM月dd日 EEE HH:mm:ss");

        System.out.println(formater.format(date));

        try {
            Date date1 = formater1.parse("2008年08月08日 星期一 08:08:08");
            System.out.println(date1.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public String getDateI() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

       return year + "-" + month + "-" + day;
    }
    public String getDateTimeI() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

    }
    public String getDateII() {
        Date date = new Date();
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
        return formater.format(date);
    }
    public String getDateTimeII() {
        Date date = new Date();
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
        return formater.format(date);
    }
     @Test
    public void test() {
      /*  fun1();
        System.getProperties().list(System.out);*/

      // date();
      //  System.out.println(trim(" abc d    lalal  "));
       // System.out.println(reverse("abcdefg",2,5));
      //  System.out.println(cntStrInOtherStr("abcdefgab","ab"));
       // System.out.println(reverseUseSB("to be or not to be"));
     // fun2("to be or not to be");
        // jvm();

         // os();
        // calendar();
        // simpleDateFormat();
         System.out.println(getDateI());
         System.out.println(getDateTimeI());
         /*System.out.println(getDateII());
         System.out.println(getDateTimeII());*/
     }
}
